//
//  WXVideoPreviewViewController.m
//  SmartVideo
//
//  Created by yindongbo on 2017/5/9.
//  Copyright © 2017年 Nxin. All rights reserved.
//

#import "WXVideoPreviewViewController.h"
@interface WXVideoPreviewViewController ()


@end

@implementation WXVideoPreviewViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
  
    [self removeAllSubView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *imgView ;
    if (self.url.length >0) {
        imgView = [[UIImageView alloc] initWithImage:[self getVideoPreViewImage]];
        [self.view addSubview:imgView];
        imgView.frame = self.view.bounds;
        
           }else {
        imgView = [[UIImageView alloc] initWithImage:self.img];
        [self.view addSubview:imgView];
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        imgView.frame = self.view.bounds;
    }
    

    
   
    
    
}
- (void)removeAllSubView {
    while (self.view.subviews.count) {
        [self.view.subviews.lastObject removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIImage*) getVideoPreViewImage {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:self.url] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *img = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return img;
}

#pragma mark - ActionMethod
- (void)clickAction:(UIButton *)btn {
    [self.view removeFromSuperview];
    if (btn.tag == 1) {
        NSLog(@"后退");
    }
    else if (btn.tag == 2){
        NSLog(@"提交");
        if (self.operateBlock) {
            self.operateBlock();
        }
    }
}

@end
